package com.rachel.activitytest;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.hardware.Camera;
import android.hardware.camera2.CameraAccessException;
import android.hardware.camera2.CameraManager;
import android.os.Build;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.LinearLayout;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zhengshaorui on 2017/8/15.
 */

public class ToolUtils {
    private static final String TAG = "zsr";
    public static final String SCAN_RESULT = "result";
    public static final String SCAN_HTTP = "https";
    public static final int SCAN_OPEN_QRCODE = 1;
    public static final int SCAN_OPEN_PHONE = 2;
    //recyclerview 的类型
    public static final int TYPE_ZERO = 0;
    public static final int TYPE_ONE = 1;
    public static final int TYPE_TWO = 2;
    public static final int TYPE_THREE = 3;




    private static List<String> getImageurl(List<String> urls, int i, int i1) {
        List<String> imageurl = new ArrayList<>();
        for (int j = i - 1; j < i1; j++) {
            if (j > -1) {
                imageurl.add(urls.get(j));
            }
        }
        return imageurl;
    }

    /**
     * 像素与密度转换
     * @param context
     * @param dpValue
     * @return
     */
    public static int dip2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }


    public static int px2dip(Context context, float pxValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (pxValue / scale + 0.5f);
    }

    /**
     * 设置状态栏颜色
     * @param activity
     * @param color
     */
    public static void setStatusBarColor(Activity activity, int color){
        //4.0 到5.0的处理方法
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT
                && Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP){
            //设置透明
            activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            //生成一个 状态栏大小的 view
            View view = createStatusBarView(activity,color);

            //添加到布局中
            ViewGroup decorView = (ViewGroup) activity.getWindow().getDecorView();
            decorView.addView(view);

            //设置布局参数

            ViewGroup rootView = (ViewGroup) ((ViewGroup)
                    activity.findViewById(android.R.id.content)).getChildAt(0);
            rootView.setFitsSystemWindows(true);
            rootView.setClipChildren(false);
            rootView.setClipToPadding(false);

        }

        //5.0 以上的处理方法
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){
            activity.getWindow()
                    .addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            //清除flag
            activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            activity.getWindow().setStatusBarColor(color);
        }

    }

    /**
     * 生成一个和状态大小一致的view
     * @param activity
     * @param color
     * @return
     */
    public static View createStatusBarView(Activity activity,int color) {
        // 绘制一个和状态栏一样高的矩形
        View view = new View(activity);
        LinearLayout.LayoutParams params =
                new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                        getStatusBarHeight(activity));
        view.setLayoutParams(params);
        view.setBackgroundColor(color);
        return view;
    }

    /**
     * 获取状态栏高度
     * @param activity
     * @return
     */
    public static int getStatusBarHeight(Activity activity){
        int resourceId = activity.getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0){

            return  activity.getResources().getDimensionPixelSize(resourceId);
        }
        return -1;
    }

    /**
     * 打开闪光灯
     */
    static Camera camera = null;
    @TargetApi(Build.VERSION_CODES.M)
    public static void openFlash(Context context, boolean open){
        //6.0 用camera2
        CameraManager manager = (CameraManager) context.getSystemService(Context.CAMERA_SERVICE);

        boolean isenablefalse = context.getPackageManager()
                .hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
        Log.d(TAG, "openFlash: "+open);

        if (open){
            if (isVersionM()){
                try {

                    String cameraId = manager.getCameraIdList()[0];
                    manager.setTorchMode(cameraId,true);
                } catch (CameraAccessException e) {
                    e.printStackTrace();
                }
            }else{
                if (camera == null){
                    camera = Camera.open();
                }
                Camera.Parameters parameters = camera.getParameters();
                parameters.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
                camera.setParameters(parameters);
                camera.startPreview();
            }
        }else{
            if (isVersionM()){
                try {
                    String cameraId = manager.getCameraIdList()[0];
                    manager.setTorchMode(cameraId,false);
                } catch (CameraAccessException e) {
                    e.printStackTrace();
                }
            }else{
                if (camera != null){
                    camera.stopPreview();
                    Camera.Parameters parameters = camera.getParameters();
                    parameters.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
                    camera.setParameters(parameters);
                    camera.release();
                    camera = null;
                }

            }
        }

    }


    private static boolean isVersionM(){
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
            return true;
        }else{
            return false;
        }
    }
}
